//
//  MainViewController.swift
//  MySinaWeibo
//
//  Created by apple on 15/11/1.
//  Copyright © 2015年 baidu. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        addChildViewControllers()
        print(tabBar.subviews)
      
        }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
//        此时tabBarButton都已经创建
        setUpComposedButton()
    }
    
    func clickComposedButton() {
    
        print(__FUNCTION__)
    }
//    设置撰写按钮位置
    private func setUpComposedButton() {
    
//        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
//        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
//        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
        
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
//    添加所有控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
//        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
//        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
//        addChildViewController(UIViewController())
//        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
//        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
//    添加子控制器
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
//        设置tintColor
//        tabBar.tintColor = UIColor.orangeColor()
        
//        let vc = HomeTableViewController()
//        title属性会从内向外传递设置
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
       
    }

//MARK: - 懒加载控件
    private lazy var composeButton: UIButton = {
    
//        自定义类型的按钮
        let button = UIButton()
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//         self.tabBar.addSubview(button)
//        button.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        button.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
 
}






















